package com.lyc.service.impl;

import com.lyc.dao.AdminInfoDAO;
import com.lyc.dao.imp.AdminInfoDAOImpl;
import com.lyc.dto.AuthDto;
import com.lyc.entity.AdminInfo;
import com.lyc.service.AdminService;
import com.lyc.util.MD5;

public class AdminServiceImpl implements AdminService {
	
	private AdminInfoDAO dao = new AdminInfoDAOImpl();

	@Override
	public AuthDto login(String name, String pwd) throws Exception {
		//参数校验
		if(name ==  null || pwd == null || "".equals(name) || "".equals(pwd)) {
			throw new Exception("用户名和密码不能空");
		}
		
		//获取用户对象
		AdminInfo admininfo = null;
		try {
			admininfo = dao.selectByName(name);
		}catch(Exception e) {
			throw new Exception("用户不存在",e);
		}
		
		//比较密码的正确性
		if(!MD5.encode(pwd).equals(admininfo.getAdminPwd())) {
			throw new Exception("密码错误");
		}
		
		AuthDto auth = new AuthDto();
		auth.setAdminId(admininfo.getAdminId());
		auth.setAdminName(admininfo.getAdminName());
		
		return auth;
	}

}
